import {createSlice} from '@reduxjs/toolkit'

//创建商品模块
const goodsSlice = createSlice({
    name:'goods',
    initialState:{
        goodsList:localStorage.getItem('goodsList')? JSON.parse(localStorage.getItem('goodsList')):[]
    },
    reducers:{
    //    添加商品
        addGoods(state,{type,payload}){
            state.goodsList.unshift({
                id:Math.random().toString(36).slice(2),
                ...payload
            })
            localStorage.setItem('goodsList',JSON.stringify(state.goodsList))
        }
    }
})
const goods = goodsSlice.reducer
export const {addGoods} = goodsSlice.actions
export default goods